Thread: Problem while executing a random number generation code [Probably a stack overflow]

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    1

    Question Problem while executing a random number generation code [Probably a stack overflow]

    This C code should generate 1 million random numbers, why is does it stop working during the execution process?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main()
    {
    
    
    
    
    FILE *ofp;
     int k=0, i=1000000;;
    int array[i];
    
    
    ofp=fopen("output.txt","w");
        while (ofp==NULL)
        {
     printf("File is corrupted or does not exist, please solve the problem and press enter to proceed");
            getchar();
    }
    
    
     srand(time(NULL));
    rand();
    
    
    for (k=0;  k<1000000; k++)
        {
        array[k] = rand()%100;
        fprintf(ofp, "%d\n", array[k]);
        }
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Variable k is of type int, but it increases to a very long value that the int data type can't represent (1000000). Check header limits.h to see the maximum value the type int (signed int) can represent. Using another integer data type might solve your problem, although I don't know what are, for example, the limits for the long data type in the case of your C implementation.

    Variable i is also initialized to a very long value, which I also believe might pose a problem.

    Vairable k is initialized to 0, but then it is re-initialized to that value in the beginning of the for statement in line 29, so one of either initializations can be omitted.
    Last edited by stdq; 04-22-2015 at 08:08 PM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    int should easily hold 1000000 these days on most popular platforms.

    Its pretty certain that the array (stored on the stack) size takes the program over its stack limit.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Just some additional comments.

    You need to make sure your code is neatly formatted and indented; something like this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
     
    int main()
    {
        FILE *ofp;
        int k=0, i=1000000;;
        int array[i];
    
        ofp=fopen("output.txt","w");
        
        while (ofp==NULL)
        {
            printf("File is corrupted or does not exist, please solve the problem and press enter to proceed");
            getchar();
        }
     
        srand(time(NULL));
        rand();
     
        for (k=0;  k<1000000; k++)
        {
            array[k] = rand()%100;
            fprintf(ofp, "%d\n", array[k]);
        }
    
        return 0;
    }
    That first "while()" should be an "if()". Otherwise, if the file fails to open (and "ofp" is NULL), you'll be in an infinite loop. Also, you should exit the program if the file fails to open.

    That "rand()" call immediately after "srand()" isn't doing anything; you don't need it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-27-2012, 03:36 AM
  2. Replies: 5
    Last Post: 10-05-2009, 10:21 AM
  3. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 02:08 PM
  4. Problem with random number generation
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 05:49 PM
  5. Random number generation
    By Lisa Mowbray in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 12:22 PM

Tags for this Thread